package com.ivi.car.module.carstate.view.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.ivi.car.R;
import com.ivi.car.base.BaseActivity;
import com.ivi.car.bean.carstate.CarPartState;
import com.ivi.car.module.carstate.model.IStateContract;
import com.ivi.car.module.carstate.presenter.StatePresenter;
import com.ivi.car.module.carstate.view.adapter.CarStateAdapter;
import com.ivi.car.module.carstate.view.fragment.CarPartStateFragment;
import com.ivi.car.module.carstate.view.fragment.FaultWarnFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Copyright
 * author chengxp
 * date 2018/3/7
 * describe
 * history
 */

public class CarStateActivity extends BaseActivity implements IStateContract.View {
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tab_state)
    TabLayout mTabState;
    @BindView(R.id.page_car_state)
    ViewPager mPageCarState;
    private CarPartStateFragment mStateFragment;
    private FragmentManager mManager;
    private CarStateAdapter mStateAdapter;
    private StatePresenter mPresenter;
    private static final int RQUEST_CODE = 904;
    private boolean isGranted;

    @Override
    public void showPartsState(ArrayList<CarPartState> list) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_car_state;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        mPresenter = new StatePresenter(this, mLifecycleProvider);
        checkPermisson();
    }

    private void loadFragment() {
        String[] titles = new String[]{"车辆状况", "故障提醒"};
        mManager = getSupportFragmentManager();
        CarPartStateFragment mStateFragment = new CarPartStateFragment();
        FaultWarnFragment mFailureWarnFragment = new FaultWarnFragment();
        ArrayList list = new ArrayList();
        list.add(mStateFragment);
        list.add(mFailureWarnFragment);
        mStateAdapter = new CarStateAdapter(mManager, titles, list);
        mPageCarState.setAdapter(mStateAdapter);
        mTabState.setupWithViewPager(mPageCarState);
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void checkPermisson() {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            loadFragment();
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RQUEST_CODE);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == RQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                loadFragment();
            }

        }
    }

    @Override
    protected void initListener() {
        super.initListener();

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
